'''添加用户页面'''
from selenium.common import NoSuchElementException
from framwork.pages.base_page import BasePage
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select


class AddUserPage(BasePage):
    '''添加用户页面元素定位'''
    add_user_btn_locator = (By.CSS_SELECTOR, "#user_form > div:nth-child(1) > div > a:nth-child(3)")
    add_username_locator = (By.ID, "name")
    username_locator = (By.XPATH, "/html/body/div[5]/div[2]/div/div[2]/div[2]/form/table/tbody/tr[1]/td[2]/input")
    add_passwd_locator = (By.ID, "password")  # 添加用户密码
    # 用户类别
    user_categories_locator = (By.XPATH, "/html/body/div[5]/div[2]/div/div[2]/div[2]/form/table/tbody/tr[3]/td[2]/select")
    #邮箱用户类别
    email_categories_locator = (By.XPATH, '//*[@id="add"]/table/tbody/tr[3]/td[2]/select')
    department_locator = (By.ID, "department2")  # 部门
    email_department_locator = (By.ID, "department")
    save_locator = (By.XPATH, "/html/body/div[5]/div[2]/div/div[2]/div[2]/form/table/tbody/tr[6]/td[2]/input[1]")
    save_new_locator = (By.CSS_SELECTOR, "input.btn.btn-primary[value='保存并新建']")
    email_invite_locator = (By.LINK_TEXT, "邮箱邀请")
    add_email_locator = (By.CSS_SELECTOR, "input[name='email']")

    add_email_success_locator = (By.CSS_SELECTOR, "body > div.container > div.alert.alert-success")
    add_email_error_locator = (By.CSS_SELECTOR, "body > div.container > div.row > div > div:nth-child(1)")

    add_user_success_locator = (By.CSS_SELECTOR, "body > div.container > div.alert.alert-success")
    add_user_error_locator = (By.CSS_SELECTOR, "body > div.container > div.row > div > div:nth-child(1)")

    def email_categories_xlk(self):
        '''用户类别下拉框'''
        select3 = Select(self.find_element(self.email_categories_locator))
        select3.select_by_index(1)

    def email_department(self):
        '''邮箱邀请部门'''
        select4 = Select(self.find_element(self.email_department_locator))
        select4.select_by_index(1)

    def click_email_invite(self):
        '''点击邮箱邀请'''
        self.find_element(self.email_invite_locator).click()

    def input_email(self, email):
        '''输入邮箱名'''
        self.find_element(self.add_email_locator).send_keys(email)

    def input_user_add_username(self, add_username):
        '''添加用户，输入用户名'''
        element = self.find_element(self.username_locator)
        element.send_keys(add_username)

    def input_add_username(self,add_username):
        '''邮箱邀请添加用户，输入用户名'''
        self.find_element(self.add_username_locator).send_keys(add_username)

    def input_add_password(self,add_password):
        self.find_element(self.add_passwd_locator).send_keys(add_password)

    def user_categories_xlk(self):
        '''用户类别下拉框'''
        select1 = Select(self.find_element(self.user_categories_locator))
        select1.select_by_index(1)

    def department_xlk(self):
        '''部门下拉框'''
        select2 = Select(self.find_element(self.department_locator))
        select2.select_by_index(1)

    def click_save(self):
        '''点击保存'''
        self.find_element(self.save_locator).click()

    def click_save_new(self):
        '''点击保存并新建'''
        self.find_element(self.save_new_locator).click()

    def add_user_seccess_text(self):
        '''添加用户成功提示文本，用于断言'''
        try:
            text = self.find_element(self.add_user_success_locator).text
        except NoSuchElementException:
            text = ""
        return text

    def add_user_error_text(self):
        '''添加用户成功提示文本，用于断言'''
        try:
            text = self.find_element(self.add_user_error_locator).text
        except NoSuchElementException:
            text = ""
        return text

    def add_email_seccess_text(self):
        '''添加用户邮箱邀请成功提示文本，用于断言'''
        try:
            text = self.find_element(self.add_email_success_locator).text
        except NoSuchElementException:
            text = ""
        return text

    def add_email_error_text(self):
        '''添加用户邮箱邀请失败提示文本，用于断言'''
        try:
            text = self.find_element(self.add_email_error_locator).text
        except NoSuchElementException:
            text = ""
        return text


